package com.everwin.web.controller.system;

import com.everwin.common.annotation.Log;
import com.everwin.common.core.controller.BaseController;
import com.everwin.common.core.domain.AjaxResult;
import com.everwin.common.core.page.TableDataInfo;
import com.everwin.common.enums.BusinessType;
import com.everwin.common.utils.poi.ExcelUtil;
import com.everwin.framework.util.ShiroUtils;
import com.everwin.system.domain.SysDictType;
import com.everwin.system.service.ISysDictTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 数据字典信息

 */
@Api("数据字典类型信息")
@Controller
@RequestMapping("/system/dict")
public class SysDictTypeController extends BaseController
{

    @Autowired
    private ISysDictTypeService dictTypeService;

    @ApiOperation("查询数据字典类型分页")
    @ApiImplicitParam(name = "sysDictType", value = "查询字典类型", dataType = "SysDictType")
    @PostMapping("/list")
    @RequiresPermissions("system:dict:list")
    @ResponseBody
    public TableDataInfo list(SysDictType dictType)
    {
        startPage();
        List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
        return getDataTable(list);
    }

    @ApiOperation("导出数据字典类型")
    @ApiImplicitParam(name = "sysDictType", value = "查询字典类型", dataType = "SysDictType")
    @Log(title = "字典类型", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:dict:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysDictType dictType)
    {

        List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
        ExcelUtil<SysDictType> util = new ExcelUtil<SysDictType>(SysDictType.class);
        return util.exportExcel(list, "字典类型");
    }


    /**
     * 新增保存字典类型
     */
    @ApiOperation("新增保存字典类型")
    @ApiImplicitParam(name = "sysDictType", value = "字典类型",required = true, dataType = "SysDictType")
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    @RequiresPermissions("system:dict:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysDictType dict)
    {
        dict.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(dictTypeService.insertDictType(dict));
    }

    /**
     * 修改保存字典类型
     */
    @ApiOperation("修改保存字典类型")
    @ApiImplicitParam(name = "sysDictType", value = "字典类型",required = true, dataType = "SysDictType")
    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:dict:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysDictType dict)
    {
        dict.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(dictTypeService.updateDictType(dict));
    }

    @ApiOperation("删除字典类型")
    @ApiImplicitParam(name = "ids", value = "字典类型ID", required = true, dataType = "String", paramType = "path")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @RequiresPermissions("system:dict:remove")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            return toAjax(dictTypeService.deleteDictTypeByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }


    /**
     * 校验字典类型
     */
    @ApiOperation("校验字典类型是否唯一")
    @ApiImplicitParam(name = "sysDictType", value = "字典类型",required = true, dataType = "SysDictType")
    @PostMapping("/checkDictTypeUnique")
    @ResponseBody
    public String checkDictTypeUnique(SysDictType dictType)
    {
        return dictTypeService.checkDictTypeUnique(dictType);
    }
}
